package ch.fhnw.kosmonauten.client.net;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import ch.fhnw.kosmonauten.model.User;
import ch.fhnw.kosmonauten.net.Message;
import ch.fhnw.kosmonauten.net.Request;
import ch.fhnw.kosmonauten.net.Response;
import ch.fhnw.kosmonauten.net.ServerBussyMessage;
import ch.fhnw.kosmonauten.net.TextMessage;

/**
 * Diese Klasse ermoeglicht die Server-Verbindung.
 * 
 * @author Fabian Siegrist
 * @author Oliver Freiermuth
 * @author Mikhail Onoufrienko
 * @author Risto Milani
 * @version $Revision: 79 $
 *          File name: $HeadURL: http://iching-kosmonauten.googlecode.com/svn/I-Ching/src/ch/fhnw/kosmonauten/client/net/ServerConnection.java $
 *          Last modified:$Date: 2013-05-24 12:35:41 +0000 (Fri, 24 May 2013) $
 *          Last modified by: $Author: molotok.mihon@gmail.com $
 */

// class definition
public class ServerConnection {

	// attributes
	private final String defaulthost = "localhost";
	private final int defaultport = 14555;
	private Socket socket;
	private ObjectOutputStream clientOut;
	private ObjectInputStream clientIn;
	private final String host;
	private final int port;

	// constructor
	public ServerConnection() {
		this.host = defaulthost;
		this.port = defaultport;
	}

	// methods
	/**
	 * @param host
	 * @param port
	 */
	public ServerConnection(String host, int port) {
		this.host = host;
		this.port = port;
	}

	private void openConnection() {
		try {
			System.out.println("opening port");
			this.socket = new Socket(this.host, this.port);
			this.clientOut = new ObjectOutputStream(
					this.socket.getOutputStream());
			this.clientIn = new ObjectInputStream(this.socket.getInputStream());
			System.out.println("connection opened");
			Message msg = (Message) this.clientIn.readObject();
			if (msg instanceof ServerBussyMessage) {
				this.socket.close();
				System.out.println("Server full... try later");
			} else {
				System.out.println("Connected to Server...");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 
	 */
	public void sendTestMsg() {
		openConnection();
		try {
			this.clientOut.writeObject(new TextMessage(
					"Hello Server, here is Client!"));
			Message msg = null;
			msg = (Message) this.clientIn.readObject();
			if (msg instanceof TextMessage) {
				System.out.println("Server sais: "
						+ msg.getContent().toString());
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			sendBy();
			closeConnection();
		}

	}

	/**
	 * @param request
	 * @return
	 */
	public Response sendRequest(Request request) {
		openConnection();
		try {
			this.clientOut.writeObject(request);
			Message msg = null;
			msg = (Message) this.clientIn.readObject();
			if (msg instanceof Response) {
				return (Response) msg;
				// System.out.println("Server sais: " + msg.getContent().toString());
			} else
				return null;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			sendBy();
			closeConnection();
		}
	}

	/**
	 * @param user
	 */
	public void checkUserLogin(User user) {
		openConnection();
		try {
			this.clientOut.writeObject(new TextMessage("login"));
			Message msg = null;
			msg = (Message) this.clientIn.readObject();
			if (msg instanceof TextMessage) {
				System.out.println("Server sais: "
						+ msg.getContent().toString());
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			sendBy();
			closeConnection();
		}
	}

	/**
	 * 
	 */
	private void sendBy() {
		// TODO Auto-generated method stub
		try {
			this.clientOut.writeObject(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 */
	private void closeConnection() {
		try {
			this.clientIn.close();
			this.clientOut.close();
			this.socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}